<!-- ^2_3^(220105) -->
<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <a-row :gutter="8">
        <a-col :span="5">
          <s-tree-menu
            :dataSource="orgTree"
            :openKeys.sync="openKeys"
            :search="false"
            @click="handleClick"
            @add="handleAdd"
            @groupAdd="handleGroupAdd"
            @groupEdit="handleGroupEdit"
            @groupHide="handleGroupHide"
            @titleClick="handleTitleClick"></s-tree-menu>
        </a-col>
        <a-col :span="19">
          <a-button type="primary" icon="plus" @click="handleMenuAdd()">添加顶级菜单</a-button>
          <s-table
            ref="table"
            size="default"
            :columns="columns"
            :data="loadData"
            :alert="false"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          >
            <span slot="action" slot-scope="text, record">
              <template>
                <a @click="handleGridEdit(record)">编辑</a>
                <a-divider type="vertical" />
              </template>
              <a-dropdown>
                <a class="ant-dropdown-link">
                  更多 <a-icon type="down" />
                </a>
                <a-menu slot="overlay">
                  <!-- <a-menu-item>
                    <a href="javascript:;">详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a href="javascript:;">隐藏/显示</a>
                  </a-menu-item> -->
                  <a-menu-item>
                    <a @click="handleGridAdd(record)">添加</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </s-table>
        </a-col>
      </a-row>

      <menu-modal ref="modal" @ok="handleSaveOk" @close="handleSaveClose" />
    </a-card>
  </page-header-wrapper>
</template>

<script>
// ^2_3^
import STreeMenu from '@/components/Tree/TreeMenu'
import { STable } from '@/components'
import MenuModal from './modules/MenuModal'
import { menuListPage, menuTree } from '@/api/menu'

export default {
  name: 'TreeList',
  components: {
    STable,
    STreeMenu,
    MenuModal
  },
  data () {
    return {
      openKeys: [1],

      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '菜单ID',
          dataIndex: 'id'
        },
        {
          title: '父级ID',
          dataIndex: 'pid'
        },
        {
          title: '菜单名称',
          dataIndex: 'title'
        },
        {
          title: '菜单标示',
          dataIndex: 'name'
        },
        {
          title: '是否显示',
          dataIndex: 'status',
          customRender: (val) => val === 1 ? '是' : '否'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // (列表数据)加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return menuListPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            if (res.code === 0) {
              return res.data
            } else {
              this.$message.error(res.msg)
              return {
                pageSize: 0,
                pageNo: 0,
                totalCount: 0,
                totalPage: 0,
                data: []
              }
            }
          })
      },
      orgTree: [],
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created () {
    // ^2_3^获取树形数据
    this.handleMenuTree()
  },
  methods: {
    // 树形菜单数据
    handleMenuTree () {
      menuTree().then(res => {
        this.orgTree = res.data
      })
    },
    // 菜单添加
    handleMenuAdd () {
      this.$refs.modal.add({ id: 0, pid: 0 }) // 弹层modal
      console.log('>handleMenuAdd(菜单添加)')
    },
    handleClick (e) {
      console.log('>handleClick', e)
      this.queryParam = {
        id: e.key
      }
      this.$refs.table.refresh(true)
    },
    handleAdd (item) {
      console.log('>handleAdd', item)
      // this.$message.info(`提示：你点了 ${item.key} - ${item.title} `)
      // this.$refs.modal.add(item.key) // 弹层modal
    },
    handleGroupAdd (item) {
      this.$refs.modal.add({ id: 0, pid: item['key'] }) // 弹层modal
      console.log('>handleGroupAdd', item)
    },
    handleGroupEdit (item) {
      this.handleGridEdit(item)
      console.log('>handleGroupEdit', item)
    },
    handleGroupHide (item) {
      console.log('>handleGroupHide', item)
    },
    handleTitleClick (item) {
      console.log('>handleTitleClick', item)
    },
    titleClick (e) {
      console.log('>titleClick', e)
    },
    handleSaveOk () {
      console.log('>handleSaveOk')
      this.$refs.table.refresh(true)
      // ^2_3^获取树形数据
      this.handleMenuTree()
    },
    handleSaveClose () {
      console.log('>handleSaveClose')
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      console.log('>selectedRowKeys', selectedRowKeys)
      console.log('>selectedRows', selectedRows)
    },
    handleGridEdit (rec) {
      const record = {
        id: rec['id'],
        pid: rec['pid'],
        title: rec['title'],
        name: rec['name'],
        is_menu: rec['is_menu'],
        status: rec['status'],
        group: rec['group'],
        icon: rec['icon'],
        component: rec['component'],
        redirect: rec['redirect'],
        path: rec['path'],
        controller: rec['controller'],
        action: rec['action'],
        is_hide_header: rec['hideHeader'],
        is_hide_children: rec['hideChildren']
      }
      this.$refs.modal.edit(record) // 弹层modal
      console.log('>handleGridEdit', record)
    },
    handleGridAdd (rec) {
      this.$refs.modal.add({ id: 0, pid: rec['id'] })
      console.log('=>handleGridAdd', rec)
    }
  }
}
</script>

<style lang="less">
  .custom-tree {

    /deep/ .ant-menu-item-group-title {
      position: relative;
      &:hover {
        .btn {
          display: block;
        }
      }
    }

    /deep/ .ant-menu-item {
      &:hover {
        .btn {
          display: block;
        }
      }
    }

    /deep/ .btn {
      display: none;
      position: absolute;
      top: 0;
      right: 10px;
      width: 20px;
      height: 40px;
      line-height: 40px;
      z-index: 1050;

      &:hover {
        transform: scale(1.2);
        transition: 0.5s all;
      }
    }
  }
</style>
